package everydayGrade.t1;

import CommonStructure.ListNode;

/**
 * @Author: Siantar
 * @Date: 2023-07-30-19:49
 * @Description: 1.0
 */
public class T0142 {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (true) {
            if (fast == null || fast.next == null) return null;
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) break;
        }
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
